<?php
/**
 * Common Password Class
 * 
 * @author 		Rafael Kutscha (r.kutscha@aikanet.de)
 * @copyright   Copyright (c) 2010 Rafael Kutscha
 */
final class Ht_Password
{
    /**
     * @desc zu verwendende Verschlüsselung
     */
    const PW_PROTECTION_MODE = 'sha1';
    /**
     * Verschlüsselt das Passwort (Einweg)
     * @param string $password
     * @return string den Verschlüsselten String
     */
    public static function cryptPassword ($password)
    {
        switch (self::PW_PROTECTION_MODE) {
            case 'sha1':
                return sha1($password);
                break;
            case 'md5':
                return md5($password);
                break;
            default:
                return sha1($password);
        }
    }
    /**
     * Generiert ein Passwort mit Sonderzeichen
     * @param int $length
     * @return string
     */
    public static function generateNewPassword ($length = 8)
    {
        $dummy = array_merge(range('0', '9'), range('a', 'z'), range('A', 'Z'));
        // Genereate passwort out of the given ranges: 
        mt_srand((double) microtime() * 1000000);
        for ($i = 1; $i <= (count($dummy) * 2); $i ++) {
            $swap = mt_rand(0, count($dummy) - 1);
            $tmp = $dummy[$swap];
            $dummy[$swap] = $dummy[0];
            $dummy[0] = $tmp;
        }
        // Remove irritating character: Oo01iIlL
        $cod = substr(implode('', $dummy), 0, $length);
        $cod = ereg_replace("0", "3", $cod);
        $cod = ereg_replace("1", "2", $cod);
        $cod = ereg_replace("I", "J", $cod);
        $cod = ereg_replace("L", "M", $cod);
        $cod = ereg_replace("O", "P", $cod);
        $cod = ereg_replace("Y", "X", $cod);
        $cod = ereg_replace("Z", "A", $cod);
        $cod = ereg_replace("i", "j", $cod);
        $cod = ereg_replace("l", "m", $cod);
        $cod = ereg_replace("o", "p", $cod);
        return $cod;
    }
}
?>